package com.lzx.controller;


import com.alibaba.fastjson.JSONObject;
import com.lzx.model.FileEntity;
import com.lzx.model.User;
import com.lzx.model.nybk.Nybk;
import com.lzx.model.zjwd.Nztj;
import com.lzx.model.zjwd.Zjwd;
import com.lzx.model.zjwd.Zjxx;
import com.lzx.model.zlzs.Commodity;
import com.lzx.model.zlzs.Photo;
import com.lzx.service.FileEntityService;
import com.lzx.service.zjwd.NztjService;
import com.lzx.service.zjwd.ZjwdService;
import com.lzx.service.zjwd.ZjxxService;
import com.lzx.service.zlzs.CommodityService;
import com.lzx.utils.SessionUtil;
import com.lzx.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.lzx.controller.IndexController.writeCookie;


/**
 * @描述 专家问答
 * @参数
 * @返回值
 * @创建人 gyl
 * @创建时间 2018/8/1
 * @修改人和其它信息
 */
@RestController
@RequestMapping("/sy/zjwd")
public class zjwdController {
    @Autowired
    ZjxxService zjxxService;

    @Autowired
    ZjwdService zjwdService;
    @Autowired
    NztjService nztjService;
    @Autowired
    private FileEntityService fileEntityService;

    /*@RequestMapping("/zjwd.html")
    public ModelAndView zjwd(HttpServletRequest request) throws SQLException {
        ModelAndView mv = new ModelAndView("system/zjwd");
        try {
            List<Zjxx> zjxx = zjxxService.selectdemolist();
            mv.addObject("zjxx", zjxx);

            List<Zjwd> zjwd = zjwdService.selectdemolist();
            mv.addObject("zjwd", zjwd);

            int titleNum=3;
            List<Zjwd> zjwdNopage = zjwdService.findZjwdNopage(new Zjwd());
            int size = zjwdNopage.size();
            if (size % 6 == 0) {
                titleNum= size/6;
            } else {
                titleNum= size/6 + 1;
            }
            mv.addObject("titleNum", titleNum);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(SessionUtil.getUser(request).getIsAdmin().equals("Y")){
            mv.addObject("isAdmn","TRUE");
        }else{
            mv.addObject("isAdmn","FALSE");
        }


        return mv;
    }*/

    @RequestMapping(value = "/zjwdxq.html")
    public ModelAndView zjwdxq(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws SQLException {
        ModelAndView mv = new ModelAndView("zhny/zhuanJiaWenDa_xx");
        //登录用户
        String locale = request.getParameter("locale");
//        System.out.println("index========"+locale);
        writeCookie(request,response,"locale",locale);
        User user = SessionUtil.getUser(request);
        mv.addObject("user", user);
        String id = request.getParameter("id");
        try {
            System.out.println("Id = " + id);
            Zjwd zjwd=zjwdService.selectdemobyid(id);
            System.out.println("zjwd = " + zjwd);
            /*zjwd.setZjname(zjxxService.selectdemobyid(zjwd.getZjid()).getZjname());*/
            mv.addObject("zjwd", zjwd);

            FileEntity fileEntity = new FileEntity();
            fileEntity.setYwId(id);
            fileEntity.setYwType("wttp");
            List<FileEntity> fileBeans = fileEntityService.selectdemolist(fileEntity);
            mv.addObject("fileBeans", fileBeans);

            List<Nztj> nztj = nztjService.selectdemolist();
            mv.addObject("nztj", nztj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(SessionUtil.getUser(request).getIsAdmin().equals("Y")){
            mv.addObject("isAdmn","TRUE");
        }else{
            mv.addObject("isAdmn","FALSE");
        }
        return mv;
    }

    /*@RequestMapping("/zjwdfy")
    public String zjwdfy(HttpServletRequest request) {
        String current = request.getParameter("current");
        if (StringUtils.isEmpty(current)) {
            current = "1";
        }
        Zjwd zjwd=new Zjwd();
        zjwd.setFrom(String.valueOf((Integer.parseInt(current) - 1) * 6));
        int titleNum=0;

        List<Zjwd> zjwdAll=new ArrayList<>();

        try {
            zjwdAll = zjwdService.findZjwdlist(zjwd);
            List<Zjwd> zjwdNopage = zjwdService.findZjwdNopage(new Zjwd());
            int size = zjwdNopage.size();
            if (size % 6 == 0) {
                titleNum= size/6;
            } else {
                titleNum= size/6 + 1;
            }




        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonstr = new JSONObject();
        jsonstr.put("zjwdAll", zjwdAll);
        jsonstr.put("titleNum", titleNum);
        jsonstr.put("current", current);
        //System.out.println(jsonstr.toJSONString());
        return jsonstr.toJSONString();
    }*/
    @RequestMapping("/zjwdnew.html")
    public ModelAndView zjwdnew(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        ModelAndView mv = new ModelAndView("zhny/zhuanJiaWenDa");
        //登录用户
        String locale = request.getParameter("locale");
//        System.out.println("index========"+locale);
        writeCookie(request,response,"locale",locale);
        User user = SessionUtil.getUser(request);

        mv.addObject("user", user);

        try {
            //专家信息
            List<Zjxx> zjxx = zjxxService.selectdemolist();
            System.out.println("zjxx = " + zjxx);
            mv.addObject("zjxx",zjxx);

            //问答介绍
            List<Zjwd> zjwd = zjwdService.selectdemolist();
            System.out.println("zjwd = " + zjwd);
            for (Zjwd wd : zjwd) {
                if(wd.getZjid() != null && wd.getZjid().length() != 0){
                    wd.setZjname(zjxxService.selectdemobyid(wd.getZjid()).getZjname());
                }
            }
            mv.addObject("zjwd",zjwd);

            //分页
            int titleNum = 3;
            List<Zjwd> zjwdNopage = zjwdService.findZjwdNopage(new Zjwd());
            int size = zjwdNopage.size();
            if(size % 6 == 0){
                titleNum = size/6;
            }else{
                titleNum = size/6 + 1;
            }
            mv.addObject("titleNum",titleNum);
        } catch (Exception e) {
            e.printStackTrace();
        }

        /*if(SessionUtil.getUser(request).getUsername().equals("system")){
            mv.addObject("isAdmn","TRUE");
        }else{
            mv.addObject("isAdmn","FALSE");
        }*/
        return mv;
    }
    public static void writeCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String value) {
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
    }
}
